package com.translate.web.service.impl;

import com.translate.web.dao.ApprovalFlowPathMapper;
import com.translate.entity.ApprovalFlowPath;
import com.translate.web.service.IApprovalFlowPathService;
import com.basic.service.impl.BaseServiceImpl;
import com.translate.vo.contract.IApprovalFlowPathVO;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Service;

@Service
public class ApprovalFlowPathServiceImpl extends BaseServiceImpl<ApprovalFlowPath> implements IApprovalFlowPathService {

	private RedisTemplate<String, List<ApprovalFlowPath>> redisTemplate;

	public ApprovalFlowPathServiceImpl(ApprovalFlowPathMapper mapper) {
		super(mapper);
	}

	protected void insertPropteris(ApprovalFlowPath data) {
		long time = System.currentTimeMillis() / 1000;
		data.setCreateTime(time);
		data.setUpdateTime(time);
	}

	protected void updatePropteris(ApprovalFlowPath data) {
		data.setUpdateTime(System.currentTimeMillis() / 1000);
	}

	protected Class<ApprovalFlowPath> getEntityClass() {
		return ApprovalFlowPath.class;
	}

	@Override
	public List<ApprovalFlowPath> getApprovalFlowPathFromCache(String entityName) {

		ValueOperations<String, List<ApprovalFlowPath>> valueOperations = this.redisTemplate.opsForValue();

		List<ApprovalFlowPath> order = valueOperations.get(entityName);

//		if (null != order) {
//			return order;
//		}

		ApprovalFlowPathMapper approvalFlowPathMapper = this.getBaseMapper();

		order = approvalFlowPathMapper.getApprovalFlowPathByEntityName(entityName);
		
		if (null == order) {
			order = new ArrayList<ApprovalFlowPath>(0);

			valueOperations.set(entityName, order, 3, TimeUnit.SECONDS);

			return order;
		}
		
		order.sort((o, f) -> o.getApprovalIndex() - f.getApprovalIndex());

		valueOperations.set(entityName, order, 120, TimeUnit.SECONDS);

		return order;

	}

	@Autowired
	public void setRedisTemplate(RedisTemplate<String, List<ApprovalFlowPath>> redisTemplate) {
		this.redisTemplate = redisTemplate;
	}

	/**
	 * 获取审批VO
	 */
	@Override
	public <VO extends IApprovalFlowPathVO> List<VO> getApprovalFlowPathFromCache(String entityName, Class<VO> target) {

		List<ApprovalFlowPath> approvalFlowPaths = this.getApprovalFlowPathFromCache(entityName);

		List<VO> vos = new ArrayList<>(approvalFlowPaths.size() * 4 / 3);

		VO vo;

		try {
			for (ApprovalFlowPath voFlowPath : approvalFlowPaths) {

				vo = target.newInstance();

				vo.setApprovalId(voFlowPath.getApprovalId().longValue());

				vo.setApprovalIndex(voFlowPath.getApprovalIndex());

				vo.setId(voFlowPath.getId());
				
				vo.setType(voFlowPath.getType());

				vos.add(vo);

			}
		} catch (Exception e) {
			e.printStackTrace();// TODO: handle exception
		}

		return vos;

	}

	
	@Override
	public boolean inspectionSequenceApprover(int userId) {
//		
//		List<ApprovalFlowPath> approvalFlowPaths = this.getApprovalFlowPathFromCache(Receivables.class.getSimpleName());
//
//		// 第一顺位审批人
////		ApprovalFlowPath firstFlowPath = approvalFlowPaths.get(0);
////		
////		if (0 == firstFlowPath.getApprovalId().compareTo(userId)) {
////			
////			if (firstFlowPath.getApprovalIndex().)
////			
////		}
//		
//		for (ApprovalFlowPath approvalFlowPath : approvalFlowPaths) {
//			
//			if (0 == approvalFlowPath.getApprovalId().compareTo(userId)) {
//				return approvalFlowPath;
//			}
//			
//		}
		
		
		return true;
	}

	@Override
	public Map<Integer, String> getApprovalSortToMap(String entityName) {
		
		List<ApprovalFlowPath> approvalFlowPaths = this.getApprovalFlowPathFromCache(entityName);
		
		// 保证顺序
		Map<Integer, String> map = new LinkedHashMap<>();
		
		for (ApprovalFlowPath approvalFlowPath : approvalFlowPaths) {
			
			map.put(approvalFlowPath.getApprovalId(), approvalFlowPath.getApprovalIndex() + "-" + approvalFlowPath.getId());
		}
		
		return map;
	}
}